package com.jwthhealth_pub.old.subscribe.address.adapter;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.common.LogUtil;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.api.model.CommonRespone;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;
import com.jwthhealth_pub.old.subscribe.SubscribeActivity;
import com.jwthhealth_pub.old.subscribe.address.SubscribeHistoryAddressActivity;
import com.jwthhealth_pub.old.subscribe.address.SubscribeProvinceActivity;
import com.jwthhealth_pub.old.subscribe.model.SubscrbeAddList;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 历史位置，适配器
 * Created by Administrator on 2017/7/18.
 */

public class SubscribeHistoryAddressAdatper extends RecyclerView.Adapter {

    private static String TAG = "HistoryAddressAdatper";

    private SubscribeHistoryAddressActivity mActivity;
    private List<SubscrbeAddList.DataBean> data;

    public SubscribeHistoryAddressAdatper(SubscribeHistoryAddressActivity activity, List<SubscrbeAddList.DataBean> data) {
        this.mActivity = activity;
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mActivity, R.layout.item_textview, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;
        final int pos = vh.getAdapterPosition();
        if (data.size() == 0 || position == data.size()) {//list+1
            vh.name.setText(mActivity.getString(R.string.subscribe_history_address_add));
//            vh.name.setTextColor(mActivity.getResources().getColor(R.color.primarycolor));
            vh.name.setTextColor(ContextCompat.getColor(mActivity, R.color.primarycolor));
            vh.img.setVisibility(View.VISIBLE);
            vh.view.setVisibility(View.GONE);
        } else {
            SubscrbeAddList.DataBean dataBean = data.get(position);
            vh.name.setText(dataBean.getCity() + " " + dataBean.getAddress());
    }

        vh.clickLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                final SubscrbeAddList.DataBean dataBean = data.get(pos);
                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                builder.setTitle("确定删除地址？");
                builder.setNegativeButton("取消", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
                        if (userinfoModel != null) {
                            Call<CommonRespone> call = ApiHelper.removeSubscribeAdd(dataBean.getId(), userinfoModel.getId(), userinfoModel.getAndroidtoken());
                            call.enqueue(new Callback<CommonRespone>() {
                                @Override
                                public void onResponse(Call<CommonRespone> call, Response<CommonRespone> response) {
                                    final CommonRespone body = response.body();
                                    mActivity.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            LogUtil.toast(body.getMsg());
                                            if (body.getCode().equals("0")) {
                                                data.remove(dataBean);
//                                                notifyDataSetChanged();
                                                mActivity.addressList.setAdapter(new SubscribeHistoryAddressAdatper(mActivity, data));
                                            }
                                        }
                                    });
                                }

                                @Override
                                public void onFailure(Call<CommonRespone> call, Throwable t) {
                                    LogUtil.e(t.toString(), TAG);
                                }
                            });
                        }
                    }
                });

                builder.show();
                return true;
            }
        });

        vh.clickLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (pos == data.size()) {
                    mActivity.startActivity(new Intent(mActivity, SubscribeProvinceActivity.class));
                } else {
                    SubscribeHistoryAddressActivity subscribeHistoryAddressActivity = new SubscribeHistoryAddressActivity();
                    subscribeHistoryAddressActivity.finish();

                    SubscrbeAddList.DataBean dataBean = data.get(pos);
                    SubscribeActivity newSubscribeActivity = new SubscribeActivity();
                    Intent intent = new Intent(mActivity, newSubscribeActivity.getClass());
                    App.preferenceUtil.putString(Constant.PROVINCENAME, dataBean.getProvince());
                    App.preferenceUtil.putString(Constant.CITYNAME, dataBean.getCity());
                    App.preferenceUtil.putString(Constant.ZONENAME, dataBean.getAddress());
                    mActivity.startActivity(intent);
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return data.size() + 1;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.text)
        TextView name;
        @BindView(R.id.img)
        ImageView img;
        @BindView(R.id.divide_view)
        View view;
        @BindView(R.id.click_layout)
        RelativeLayout clickLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
